const app = getApp()
let qCloud = require("../../index.js");
let timerInterval = null;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        role: 2,
        codeDisabled: false,
        codeText: "获取验证码",
        countDown: 60,
        maxTime: 60,
        showModalStatus: false,
        organList: [],
        enterpriseId: null,
        enterpriseName: '',

        registerForm: {
            registerType: "2",
            organId: null,
            organName: null,
            realName: "",
            phone: "",
            code: "",
            password: ""
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.registerForm = {
            registerType: "2",
            organId: null,
            organName: "",
            realName: "",
            phone: "",
            code: "",
            password: ""
        }
    },

    roleTap(e) {
        let self = this
        let role = e.currentTarget.dataset.role
        self.setData({
            role: role,
            enterpriseId: null,
            enterpriseName: ''
        })
        this.data.registerForm.registerType = this.data.role;
    },

    organNameInput: function (e) {
        this.data.registerForm.organName = e.detail.value;
    },

    // 查找申请加入企业
    searchEnterprise() {
        // 不默认查询，不平铺全部公司，根据搜索内容查询
        // let params = {}
        // this.getOrganListAll(params);

        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });

        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
            showModalStatus: true
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export()
            });
        }.bind(this), 200);
    },

    hideEnterpriseModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                showModalStatus: false
            })
        }.bind(this), 200);
        this.setData({
            organList: []
        })
    },

    getOrganListAll(params) {
        let self = this;
        // 发送验证码
        app.fetchNoTokenApis(self, qCloud.api.getOrganListAll_1, params, "GET", function (e) {
            if (e.data.code == 200) {
                let result = e.data.data;
                let organList = [];
                for (let i = 0; i < result.length; i++) {
                    organList.push({
                        organId: result[i].deptId,
                        organName: result[i].deptName,
                    })
                }
                self.setData({
                    organList: organList
                })
            }
        })
    },

    searchInput: function (e) {
        let self = this;
        if(e.detail.value){
            let params = {
                deptName: e.detail.value
            };
            self.getOrganListAll(params);
        }else{
            self.setData({
                organList: []
            });
        }
    },

    // 确认选择企业
    confirmEnterprise(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({
            enterpriseName: this.data.organList[index].organName,
            showModalStatus: false
        })

        this.data.registerForm.organId = this.data.organList[index].organId;
        this.data.registerForm.organName = this.data.organList[index].organName;
    },

    realNameInput: function (e) {
        this.data.registerForm.realName = e.detail.value;
    },

    phoneInput: function (e) {
        this.data.registerForm.phone = e.detail.value;
    },

    bindSendCodeClick: function (e) {
        if (this.data.registerForm.phone == "") {
            wx.showToast({
                title: "请输入手机号码",
                icon: "error"
            })
            return;
        }
        let self = this;
        let params = {
            type: "1",
            phone: qCloud.crypto.encrypt(this.data.registerForm.phone)
        };

        // 发送验证码
        app.fetchNoTokenApis(self, qCloud.api.smsCode, params, "POST", function (e) {
            if (e.data.code != 200) {
                if (timerInterval) {
                    clearTimeout(timerInterval);
                }
                wx.showModal({
                    content: e.data.msg
                });
            } else {
                timerInterval = setTimeout(self.refreshCode, 1000);
            }
        })
    },

    refreshCode: function () {
        let self = this;
        if (self.data.countDown > 0) {
            self.setData({
                codeDisabled: true,
                countDown: self.data.countDown - 1,
                codeText: self.data.countDown + "s后重试"
            })
            timerInterval = setTimeout(self.refreshCode, 1000);
        } else {
            self.setData({
                codeDisabled: false,
                codeText: "重新获取",
                countDown: self.data.maxTime,
            })
            if (timerInterval) {
                clearTimeout(timerInterval);
            }
        }
    },

    codeInput: function (e) {
        this.data.registerForm.code = e.detail.value;
    },

    passwordInput: function (e) {
        this.data.registerForm.password = e.detail.value;
    },

    registerSubmit: function () {
        if (this.data.registerForm.organName == "") {
            let showTitle = "请输入企业名称";
            if (this.data.registerForm.registerType == "2") {
                showTitle = "请输入申请加入企业名称";
            }
            wx.showToast({
                title: showTitle,
                icon: "error"
            })
            return;
        }

        if (this.data.registerForm.realName == "") {
            wx.showToast({
                title: "请输入真实姓名",
                icon: "error"
            })
            return;
        }

        if (this.data.registerForm.phone == "") {
            wx.showToast({
                title: "请输入手机号",
                icon: "error"
            })
            return;
        }

        if (this.data.registerForm.code == "") {
            wx.showToast({
                title: "请输入验证码",
                icon: "error"
            })
            return;
        }

        if (this.data.registerForm.password == "") {
            wx.showToast({
                title: "请设置账号密码",
                icon: "error"
            })
            return;
        }

        let params = {
            registerType: this.data.registerForm.registerType,
            organId: this.data.registerForm.organId,
            organName: this.data.registerForm.organName,
            realName: this.data.registerForm.realName,
            phone: qCloud.crypto.encrypt(this.data.registerForm.phone),
            code: this.data.registerForm.code,
            password: qCloud.crypto.encrypt(this.data.registerForm.password),
            loginType: '1',
            encryptType: "1",
            userName: qCloud.crypto.encrypt(this.data.registerForm.phone)
        };

        let self = this;
        app.fetchNoTokenApis(self, qCloud.api.register, params, "POST", function (e) {
            if (e.data.code == 200) {
                if (self.data.registerForm.registerType == "2") {
                    wx.showModal({
                        title: '提示',
                        content: "注册成功！点击确定进行登录",
                        complete: (res) => {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: "/pages/login/index"
                                })
                            }
                            if (res.cancel) {
                                wx.redirectTo({
                                    url: "/pages/login/index"
                                })
                            }
                        }
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: "注册成功！点击确定进行登录",
                        complete: (res) => {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: "/pages/login/index"
                                })
                            }
                            if (res.cancel) {
                                wx.redirectTo({
                                    url: "/pages/login/index"
                                })
                            }
                        }
                    })
                }
            } else {
                qCloud.util.showSelfModal(self, e.data.msg);
            }
        })
    },
})
